package mclight.selectors;
import flash.display.DisplayObjectContainer;
import flash.events.Event;
import mclight.containers.Container;
import mclight.text.Label;
import mclight.ValueComponent;

class UISlider extends ValueComponent
{
	var _labelWidth:Int;
	
	public function new(
		?parent:DisplayObjectContainer, ?container:Container, x:Float = 0, y:Float = 0,
		label:String = "", defaultEventHandler:Event->Dynamic = null
	)
	{
		_labelWidth = 50;
		_precision = 1;
		_labelText = label;
		super(parent, container, x, y);
		if(defaultEventHandler != null)
		{
			//addEventListener(Event.CHANGE, defaultEventHandler);
		}
		formatValueText();
	}
	
	var _label:Label;
	
	var _valueLabel:Label;
	
	var _slider:Slider;
	
	var _sliderClass:Class<Slider>;
	
	override function construct()
	{
		_slider = Type.createInstance(_sliderClass, []);
		_label = new Label(_sprite, 0, 0);
		trace(_slider);
		_slider.addTo(_sprite);
		//_slider.addEventListener(Event.CHANGE, onSliderChange);
		_valueLabel = new Label(_sprite);
	}
	
	function formatValueText()
	{
		var mult:Int = Std.int(Math.pow(10, _precision));
		var val:String = Std.string(Math.round(_slider.value * mult) / mult);
		var parts = val.split(".");
		if(parts[1] == null)
		{ 
			if(_precision > 0)
			{
				val += ".";
			}
			for(i in 0 ... _precision)
			{
				val += "0";
			}
		}
		else
		{
			for(i in 0 ... _precision - parts[1].length)
			{
				val += "0";
			}
		}
		_valueLabel.text = val;
		positionValueLabel();
	}
	
	function positionValueLabel()
	{
		
	}
	
	// PUBLIC METHODS
	
	public function setSliderParams(min:Float, max:Float, value:Float)
	{
		_slider.setSliderParams(min, max, value);
	}
	
	// HANDLERS
	
	function onSliderChange(event:Event)
	{
		formatValueText();
		//dispatchEvent(new Event(Event.CHANGE));
	}
	
	// PROPERTIES
	
	override function setValue(v:Float)
	{
		_slider.value = v;
		formatValueText();
		return v;
	}
	override function getValue()
	{
		return _slider.value;
	}
	
	override function setMaxValue(m:Float)
	{
		_slider.maxValue = m;
		return m;
	}
	override function getMaxValue()
	{
		return _slider.maxValue;
	}
	
	override function setMinValue(m:Float)
	{
		_slider.minValue = m;
		return m;
	}
	override function getMinValue()
	{
		return _slider.minValue;
	}
	
	public var labelPrecision(getLabelPrecision, setLabelPrecision):Int;
	var _precision:Int;
	function setLabelPrecision(decimals:Int)
	{
		_precision = decimals;
		return decimals;
	}
	function getLabelPrecision()
	{
		return _precision;
	}
	
	public var label(getLabel, setLabel):String;
	var _labelText:String;
	function setLabel(str:String)
	{
		_labelText = str;
		draw();
		return str;
	}
	function getLabel()
	{
		return _labelText;
	}
}